package com.aegis.api.auth.service;

import com.aegis.api.auth.model.WxPermission;
import com.aegis.api.auth.model.WxRole;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
@Service
public class FeignAuthApiService {

    /**
     * 获取所有权限
     *
     * @param url 动态请求的服务地址
     * @return 权限列表
     */
    public List<WxPermission> getAllPermission(String url) {
        WxAuthApi client = createFeignClient(url);
        return client.getAllPermission();
    }

    /**
     * 获取用户的权限
     *
     * @param url    动态请求的服务地址
     * @param userId 用户ID
     * @return 权限列表
     */
    public List<WxPermission> getUserPermission(String url, Object userId) {
        WxAuthApi client = createFeignClient(url);
        return client.getUserPermission(userId);
    }

    /**
     * 获取用户的角色
     *
     * @param url    动态请求的服务地址
     * @param userId 用户ID
     * @return 角色列表
     */
    public List<WxRole> getUserRole(String url, Object userId) {
        WxAuthApi client = createFeignClient(url);
        return client.getUserRole(userId);
    }

    /**
     * 创建 Feign 客户端
     *
     * @param url 动态请求的服务地址
     * @return WxAuthApi 实例
     */
    private WxAuthApi createFeignClient(String url) {
        return Feign.builder()
                .encoder(new JacksonEncoder())  // 适用于 JSON
                .decoder(new JacksonDecoder())
                .target(WxAuthApi.class, url);
    }
}
